<?php

namespace app\common\model;
use think\Model;
use think\Db;
/**
 * [NodeSon  子菜单表]
 * @author [C。]
 * @return html
 */
class NodeSon extends Model
{
    protected $name = "auth_rule_son";

    /**
     * [getNodeInfo  获取节点数据]
     * @author [C。]
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     */
    public function getroleid($map,$field)
    {
        $result =  $this->field($field)->where($map)->select()->toArray();
        return $result;
    }
    /**
     * [getNodeInfo  获取节点数据]
     * @author [C。]
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNodeInfo($id)
    {
        $result = $this->field('id,title,pid')->select();
        $str = "";
        $role = new UserType();
        $rule = $role->getRuleById($id);

        if(!empty($rule)){
            $rule = explode(',', $rule);
        }
        foreach($result as $key=>$vo){
            $str .= '{ "id": "' . $vo['id'] . '", "pId":"' . $vo['pid'] . '", "name":"' . $vo['title'].'"';

            if(!empty($rule) && in_array($vo['id'], $rule)){
                $str .= ' ,"checked":1';
            }

            $str .= '},';
        }

        return "[" . substr($str, 0, -1) . "]";
    }


    /**
     * [getMenu  根据节点数据获取对应的菜单]
     * @author [C。]
     * @param string $nodeStr
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMenu($nodeStr = '')
    {
        //超级管理员没有节点数组
        $where = empty($nodeStr) ? 'status = 1' : 'status = 1 and id in('.$nodeStr.')';
        $result = Db::name('auth_rule_son')->where($where)->order('sort')->select();
        $menu = prepareMenu($result);
        return $menu;
    }

    /**
     * [getMenu  根据节点数据获取对应的菜单]
     * @author [C。]
     * @param string $nodeStr
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNode($id,$pid = 0)
    {
        $role = new UserTypeSon();
        $rule = $role->getRuleById($id);
        $result = $this->field('id,title,pid')->where('pid',0)->select()->toArray();

        if(!empty($rule)){
            $rule = explode(',', $rule);
        }

        foreach($result as $key=>$vo){
            if(!empty($rule) && in_array($vo['id'], $rule))
                $result[$key]['checked'] = 1;
            $result2 = $this->field('id,title,pid')->where('pid',$vo['id'])->select();
            $result[$key]['lv'] = $result2;
            if($result2){
                foreach($result2 as $k=>$v){
                    if(!empty($rule) && in_array($v['id'], $rule))
                        $result[$key]['lv'][$k]['checked'] = 1;
                    $result3 = $this->field('id,title,pid')->where('pid',$v['id'])->select();
                    $result[$key]['lv'][$k]['lv'] = $result3;
                    if($result3) {
                        foreach ($result3 as $k1 => $v1) {
                            if(!empty($rule) && in_array($v1['id'], $rule))
                                $result[$key]['lv'][$k]['lv'][$k1]['checked'] = 1;
                        }
                    }
                }
            }
        }
        return $result;
    }
}